import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:universal_html/html.dart' as uHtml;
import '../../constants.dart';
import '../../controller/evaluation.dart';
import '../../models/book/BookArguments.dart';

class EvaBookDialog extends StatelessWidget {
  EvaBookDialog({
    Key? key,
    required this.title,
    required this.message,
    required this.level,
    required this.from,
    required this.userName,
    //required this.books,
  }) : super(key: key);

  final String title;
  final String message;
  final String level;
  final String from;
  final String userName;
  final box = Hive.box('appconfig');

  @override
  Widget build(BuildContext context) {
    final EvaluationController _evaluationController = Get.find();
    // _evaluationController.fetchEvaluationRandomBook(this.level);

    //BookItem item = new BookItem();
    // item.bookContentLink =
    //     _evaluationController.evaluationRandomBook.value.bookContentLink ?? '';
    // item.bookCode =
    //     _evaluationController.evaluationRandomBook.value.bookCode ?? '';
    // item.bookId = _evaluationController.evaluationRandomBook.value.bookId ?? '';
    // _bookController.setCurrentBook(item);
    return Obx(() {
      String bid =
          _evaluationController.evaluationRandomBook.value.bookId ?? '';
      String btitle =
          _evaluationController.evaluationRandomBook.value.title ?? '';
      String bcode =
          _evaluationController.evaluationRandomBook.value.bookCode ?? '';
      String bimage = _evaluationController.evaluationRandomBook.value.image ??
          'https://resources.ichinesereader.com/books/$bcode/0.gif';

      return AlertDialog(
        title: Text(this.title),
        titleTextStyle: TextStyle(
            fontFamily: 'Montserrat',
            letterSpacing: 1,
            color: Get.isDarkMode
                ? Get.theme.primaryColorDark
                : Get.theme.primaryColorLight,
            fontSize: 27.0),
        backgroundColor: Get.theme.primaryColor,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(20))),
        content: Container(
          height: 350,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                this.message,
                style: TextStyle(
                    color: Get.isDarkMode
                        ? Get.theme.primaryColorDark
                        : Get.theme.primaryColorLight,
                    fontSize: 18.0),
              ),
              new GestureDetector(
                onTap: () {
                  Navigator.of(context).pop();
                  if (GetPlatform.isWeb) {
                    uHtml.window.localStorage['bookBasePath'] =
                        'https://resources.ichinesereader.com/books/$bcode/assets';
                    uHtml.window.localStorage['bookCode'] = bcode;
                    uHtml.window.localStorage['bookID'] = bid;
                  }

                  if ((GetPlatform.isDesktop || GetPlatform.isMacOS) &&
                      !GetPlatform.isWeb) {
                    launchInWebViewWithJavaScript(
                        '${Constants.BaseURL}icr/student/book/$bid');
                  } else if (GetPlatform.isWeb) {
                    Navigator.pushNamed(context, '/student/book',
                        arguments: BookArguments(bid, bcode, btitle, null,
                            NanhaiBookReadingTypes.evaluation, userName));
                  } else {
                    String viewMode = _evaluationController
                            .evaluationRandomBook.value.viewMode ??
                        'LANDSCAPE';
                    box.put(CacheManagerKey['_viewMode'].toString(), viewMode);

                    Navigator.pushNamed(context, '/student/book',
                        arguments: BookArguments(bid, bcode, btitle, null,
                            NanhaiBookReadingTypes.evaluation, userName));
                  }
                },
                child: Container(
                  child: Tooltip(
                    message: '',
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(12)),
                    textStyle: TextStyle(fontSize: 14, color: Colors.black),
                    preferBelow: false,
                    // child: NetworkImageModified(
                    //   bimage,
                    // ),
                    child: Image.network(
                      bimage,
                      height: 200.0,
                      width: 156,
                      fit: BoxFit.cover,
                      errorBuilder: (context, error, stackTrace) {
                        return Image.asset(
                            'assets/images/imagenotavailable.png',
                            height: 200.0,
                            width: 156,
                            fit: BoxFit.cover);
                      },
                    ),
                  ),
                  decoration: BoxDecoration(
                    color: Colors.grey[50],
                    boxShadow: [
                      BoxShadow(
                        color: Color.fromARGB(26, 236, 197, 21),
                        spreadRadius: 4,
                        blurRadius: 2,
                        offset: Offset(2, 2),
                      ),
                    ],
                  ),
                ),
              ),
              Container(
                height: 40,
                margin: const EdgeInsets.only(top: 6.0),
                child: new GestureDetector(
                  onTap: () {
                    Navigator.of(context).pop();
                    if (GetPlatform.isWeb) {
                      uHtml.window.localStorage['bookBasePath'] =
                          'https://resources.ichinesereader.com/books/$bcode/assets';
                      uHtml.window.localStorage['bookCode'] = bcode;
                      uHtml.window.localStorage['bookID'] = bid;
                    }

                    if ((GetPlatform.isDesktop || GetPlatform.isMacOS) &&
                        !GetPlatform.isWeb) {
                      launchInWebViewWithJavaScript(
                          '${Constants.BaseURL}icr/student/book/$bid');
                    } else if (GetPlatform.isWeb) {
                      Navigator.pushNamed(context, '/student/book',
                          arguments: BookArguments(bid, bcode, btitle, null,
                              NanhaiBookReadingTypes.evaluation, userName));
                    } else {
                      Navigator.pushNamed(context, '/student/book',
                          arguments: BookArguments(bid, bcode, btitle, null,
                              NanhaiBookReadingTypes.evaluation, userName));
                    }
                  },
                  child: Text.rich(TextSpan(children: <InlineSpan>[
                    TextSpan(
                      text: ' $btitle',
                      style: Theme.of(context).textTheme.bodySmall,
                    )
                  ])),
                ),
              ),
              // can't close in level reading
              //if (from == 'library')
              MaterialButton(
                  color: Get.theme.primaryColorDark,
                  onPressed: () {
                    //_bookController.refreshLevelPercentage();
                    Navigator.of(context).pop();
                  },
                  child: Text('Close'.tr,
                      style: TextStyle(
                          color: Get.isDarkMode
                              ? Colors.black
                              : Get.theme.primaryColorLight)))
            ],
          ),
        ),
      );
    });
  }
}
